package com.spike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spike.mapper.BookMapper;
import com.spike.model.Book;
import com.spike.model.User;
import com.spike.service.BooksService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
@AllArgsConstructor
@Service
public class BooksServiceImpl extends ServiceImpl<BookMapper,Book> implements BooksService {
    private final BookMapper bookMapper;
    @Override
    public Integer addBook(Book book) {
        bookMapper.addBook(book);
        return book.getBookId();
    }

    @Override
    public Boolean updateBook(Book book, Integer bookId) {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Book::getBookId,bookId);
        int update = bookMapper.update(book, wrapper);
        if (update > 0){
            return true;
        }else
            return false;
    }
    @Override
    public Boolean delete(Integer bookId, Integer version) {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Book::getBookId, bookId);
        wrapper.eq(Book::getVersion, version);
        int delete = bookMapper.delete(wrapper);
        if (delete == 1) {
            return true;
        }else
            return false;
    }
}
